package com.topscomm.sys;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.topscomm.cbo.pojo.CboDeptEntity;
import com.topscomm.cbo.pojo.CboUserEntity;
import com.topscomm.cbo.service.impl.CboDeptService;
import com.topscomm.mis.MisController;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.tap.TapController;
import com.topscomm.tap.common.SessionUserBean;

public class SysController<T extends SysEntity, TService extends ISysService<T, ?>>
		extends MisController<T, TService> implements ISysController<T, TService> {
	public String getDeptcodeHeader(SessionUserBean userBean) {
		CboDeptService cboDeptService = (CboDeptService) SpringUtil.getBean("cboDeptService");
		CboUserEntity user = userBean.getUser();
		CboDeptEntity dept = cboDeptService.queryById("" + user.getDeptid());
		String deptids_header = "", deptcodes_header = "", deptids_user = "0";
		// 所在的所有部门列表
		List<Map<String, Object>> list = cboDeptService.selectExecute("select id,orgid,headerids,code from CboDept where enabled=1");
		for (Map ent : list) {
			if (dept.getCode().startsWith(ConvertUtil.convertToString(ent.get("code"))) && dept.getOrgid() == ConvertUtil.convertToLong(ent.get("orgid")))
				deptids_user += "," + ConvertUtil.convertToString(ent.get("id"));
			if (StringUtil.isEmpty(ConvertUtil.convertToString(ent.get("headerids"))))
				continue;
			if (ConvertUtil.convertToString(ent.get("headerids")).indexOf("" + user.getId()) >= 0) {
				String deptcode = ConvertUtil.convertToString(ent.get("code"));
				for (Map ent2 : list) {
					if (ConvertUtil.convertToString(ent2.get("code")).startsWith(deptcode)) {
						deptids_header += "," + ConvertUtil.convertToString(ent2.get("id"));
						deptcodes_header += ",'" + ConvertUtil.convertToString(ent2.get("code")) + "'";
					}
				}
			}
		}
		if (deptids_header.startsWith(",")) {
			deptids_header = deptids_header.substring(1);
			deptcodes_header = deptcodes_header.substring(1);
		}
		return deptcodes_header;
	}

	public String ClobToString(Clob clob) throws SQLException, IOException {
		String reString = "";
		if (clob != null) {
			Reader is = clob.getCharacterStream();// 得到流
			BufferedReader br = new BufferedReader(is);
			String s = br.readLine();
			StringBuffer sb = new StringBuffer();
			while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
				sb.append(s);
				s = br.readLine();
			}
			reString = sb.toString();
		}
		return reString;
	}
}
